package com.ydgk.jishuzhan.controller;

import com.ydgk.jishuzhan.beans.*;
import com.ydgk.jishuzhan.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.Comparator;
import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private TalkService talkService;

    @Autowired
    private CommentService commentService;


    @RequestMapping("toQuestion")
    public String toQuestion(Model model){
        List<Question> questions = questionService.findQuestions();
        model.addAttribute("questions",questions);
        return "question";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        List<User> userList = userService.findAll();
        model.addAttribute("users", userList);
        return "question-add";
    }

    @RequestMapping("/doAdd")
    public String doAdd(Question question, Model model) {
        questionService.save(question);
        List<Blog> blogs=blogService.findBlogs();
        List<Question> questions=questionService.findQuestions();
        List<Talk> talks=talkService.findTalks();
        model.addAttribute("blogs",blogs);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        return "user-home-for-me";

    }

    @RequestMapping("/toQuestion-detail")
    public String toQuestionDetail(Integer id,Model model){
        Question question = questionService.findQuestion(id);
        question.getQuestionInfo().setUser(userService.findUserById(question.getUserId()));
        model.addAttribute("question",question);
        List<Question> questions = questionService.findQuestions();
        model.addAttribute("questions",questions);
        List<Comment> comments=commentService.findCommentsByQId(id);
        model.addAttribute("comments",comments);
        int size = comments.size();
        model.addAttribute("commentSize",size);

        int collectionTotal=questionService.findCollections(id);
        int collection=questionService.findCollection(question.getUserId(),id);
        model.addAttribute("collectionTotal",collectionTotal);
        model.addAttribute("collection",collection);
        return "question-detail";
    }

    @ResponseBody
    @RequestMapping("/collect")
    public AjaxResult collect(@Param("userId")Integer userId, @Param("questionId") Integer questionId){
        AjaxResult ajaxResult = new AjaxResult();
        int i=questionService.findCollection(userId,questionId);
        if (i==0){
            try{
                questionService.collectionAdd(userId,questionId);
                System.out.println();
                ajaxResult.setSuccess(true);
            }catch (Exception e){
                ajaxResult.setSuccess(false);
                e.printStackTrace();
            }
        }else {
            ajaxResult.setSuccess(false);
        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteQCollection")
    public AjaxResult deleteById(@RequestParam("id") Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            questionService.deleteQCollection(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }

        return ajaxResult;
    }
}